import request from '@/utils/request'

export function listBuckets() {
  return request({
    url: '/race/minio/buckets',
    method: 'get'
  })
}

export function createBucket(bucket) {
  return request({
    url: '/race/minio/bucket',
    method: 'post',
    params: { bucket }
  })
}

export function deleteBucket(bucket) {
  return request({
    url: `/race/minio/bucket/${encodeURIComponent(bucket)}`,
    method: 'delete'
  })
}

export function listObjects(params) {
  return request({
    url: '/race/minio/objects',
    method: 'get',
    params
  })
}

export function deleteObject(bucket, object) {
  return request({
    url: '/race/minio/object',
    method: 'delete',
    params: { bucket, object }
  })
}

export function uploadToBucket(bucket, file) {
  const formData = new FormData()
  formData.append('bucket', bucket)
  formData.append('file', file)
  return request({
    url: '/race/minio/uploadTo',
    method: 'post',
    data: formData,
    headers: { 'Content-Type': 'multipart/form-data' }
  })
}

export function presign(bucket, object, expires) {
  return request({
    url: '/race/minio/presign',
    method: 'get',
    params: { bucket, object, expires }
  })
}
